Imports System.Threading
Public Class MServer
    Inherits System.Windows.Forms.Form

    Dim cha1 As MemChannel
    Dim th1 As Thread
    Dim nummsg As Integer
    Dim lastnummsg As Integer
    Dim lastmsg As String
    Dim peekPerSec As Integer

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents txtMSG As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents LblRun As System.Windows.Forms.Label
    Friend WithEvents btnRun As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtName As System.Windows.Forms.TextBox
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.txtMSG = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.LblRun = New System.Windows.Forms.Label
        Me.btnRun = New System.Windows.Forms.Button
        Me.Label2 = New System.Windows.Forms.Label
        Me.txtName = New System.Windows.Forms.TextBox
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.SuspendLayout()
        '
        'txtMSG
        '
        Me.txtMSG.Location = New System.Drawing.Point(8, 88)
        Me.txtMSG.Multiline = True
        Me.txtMSG.Name = "txtMSG"
        Me.txtMSG.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.txtMSG.Size = New System.Drawing.Size(544, 144)
        Me.txtMSG.TabIndex = 0
        Me.txtMSG.Text = ""
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 64)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(72, 24)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Last MSG"
        '
        'LblRun
        '
        Me.LblRun.Location = New System.Drawing.Point(8, 16)
        Me.LblRun.Name = "LblRun"
        Me.LblRun.Size = New System.Drawing.Size(376, 32)
        Me.LblRun.TabIndex = 2
        '
        'btnRun
        '
        Me.btnRun.Location = New System.Drawing.Point(400, 256)
        Me.btnRun.Name = "btnRun"
        Me.btnRun.Size = New System.Drawing.Size(144, 40)
        Me.btnRun.TabIndex = 4
        Me.btnRun.Text = "Start"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(24, 264)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(112, 24)
        Me.Label2.TabIndex = 5
        Me.Label2.Text = "Channel Name"
        '
        'txtName
        '
        Me.txtName.Location = New System.Drawing.Point(136, 264)
        Me.txtName.Name = "txtName"
        Me.txtName.Size = New System.Drawing.Size(144, 22)
        Me.txtName.TabIndex = 6
        Me.txtName.Text = "MSERVER"
        '
        'Timer1
        '
        Me.Timer1.Enabled = True
        Me.Timer1.Interval = 200
        '
        'MServer
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
        Me.ClientSize = New System.Drawing.Size(560, 312)
        Me.Controls.Add(Me.txtName)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.btnRun)
        Me.Controls.Add(Me.LblRun)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.txtMSG)
        Me.Name = "MServer"
        Me.Text = "MServer"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub MServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LblRun.Text = "Stopped"

    End Sub

    Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
        If th1 Is Nothing Then
            btnRun.Text = "Stop"
            th1 = New Thread(AddressOf thRun)
            th1.Start()
        Else
            cha1.putmsgwait("SYSEND", 10000)
        End If
    End Sub

    Private Sub thRun()
        cha1 = New MemChannel(txtName.Text)
        nummsg = 0
        lastnummsg = 0
        peekpersec = 0
        LblRun.Text = "BuffSize=" + cha1.bufSize.ToString
        While True
            lastmsg = cha1.getMsg
            'Thread.Sleep(0)
            If lastmsg.Equals("SYSEND") Then
                Exit While
            End If
            nummsg += 1
        End While
        cha1.close()
        cha1 = Nothing
        th1 = Nothing
        btnRun.Text = "Start"
        LblRun.Text = "Stopped"


    End Sub

    Private Sub txtMSG_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMSG.TextChanged

    End Sub

    Private t1 As Long = 0
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim msgPerSec As Integer
        Dim t2 As Long
        If th1 Is Nothing Then
            Exit Sub
        End If
        t2 = Environment.TickCount
        If t1 = 0 Then
            t1 = t2
            Exit Sub
        End If

        msgPerSec = (nummsg - lastnummsg) / ((t2 - t1) / 1000)
        If msgPerSec > peekPerSec Then peekPerSec = msgPerSec
        LblRun.Text = "Total msg:" + nummsg.ToString + " Msg/s:" + msgPerSec.ToString + " Peek Msg/s:" + peekPerSec.ToString
        txtMSG.Text = lastmsg
        lastnummsg = nummsg
        t1 = Environment.TickCount

    End Sub

    Private Sub MServer_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        If Not th1 Is Nothing Then
            cha1.putMsg("SYSEND")
        End If
    End Sub
End Class
